package com.base.starter.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
* @Description: 进入swagger路径为：http://localhost:9765/swagger-ui
* @Param:
* @return:
* @Author: zhou minghao
* @Date: 10:31 2024/4/18 
*/ 
@EnableOpenApi
@Configuration
public class Swagger3Config {

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("更多请咨询服务开发者。")
                .contact(new Contact("xxx。", "http://www.xxxxx.cn", "xxxxxx@foxmail.com"))
                .version("1.0")
                .build();
    }

    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("base")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.base.starter"))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(
                        globalOperation());
    }

    private List<RequestParameter> globalOperation(){
        //添加head参数配置start
        RequestParameterBuilder tokenPar = new RequestParameterBuilder();
        List<RequestParameter> pars = new ArrayList<>();
        //第一个token为传参的key，第二个token为swagger页面显示的值
        tokenPar.in(ParameterType.HEADER).name("X-TOKEN").description("令牌").required(true).query(param -> param.model(model -> model.scalarModel(ScalarType.STRING))).build();
        pars.add(tokenPar.build());
        return pars;
    }

}
